package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Menu;
import com.itheima.service.MenuService;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @ClassName MenueController
 * @Description TODO
 * @Author sy
 * @Date 2019/12/12 9:22
 * @Version 1.0
 **/
@RestController
@RequestMapping("menu")
public class MenuController {
    @Reference
    private MenuService menuService;
    @RequestMapping("findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
       PageResult pageResult= menuService.findPage(queryPageBean);
       return pageResult;
    }

    //新增权限
    @RequestMapping("add")
    public Result add(@RequestBody Menu menu){
        try {
            menuService.add(menu);
            return new Result(true, MessageConstant.ADD_MENU_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(true, MessageConstant.ADD_MENU_FAIL);
        }
    }
    //回显数据
    @RequestMapping("findById")
    public Result findById(Integer id){
        try {
            Menu menu=menuService.findById(id);
            return new Result(true,MessageConstant.QUERY_MENU_SUCCESS,menu);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_MENU_FAIL);
        }
    }
    //权限编辑
    @RequestMapping("edit")
    public Result edit(@RequestBody Menu menu){
        try {
            menuService.edit(menu);
            return new Result(true,MessageConstant.EDIT_MENU_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(true,MessageConstant.EDIT_MENU_FAIL);
        }
    }
    @RequestMapping("deleteById")
    public Result deleteById(Integer id){
        try {
            menuService. deleteById(id);
            return new Result(true,MessageConstant.DELETE_MENU_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(true,MessageConstant.DELETE_MENU_FAIL);
        }
    }

    @RequestMapping("showMenu")
    public Result showMenu(){
        try {
            SecurityContext context = SecurityContextHolder.getContext();
            Authentication authentication = context.getAuthentication();
            org.springframework.security.core.userdetails.User user = (User) authentication.getPrincipal();
            String username = user.getUsername();
            List<Menu> menuList=menuService.showMenu(username);
            return new Result(true, MessageConstant.GET_MENULIST_SUCCESS,menuList);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.GET_MENULIST_FAIL);
        }
    }
}
